<div class="row">
    <nav class="navbar navbar-default navbar-static-top">
        <div class="page-title">修改产品</div>
    </nav>
    <div class="show-content">
        <form>
            <div class="form-horizontal">
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i> 游戏类别</label>
                    <label class="col-lg-8">
                        <fieldset disabled>
                            <select id="class" name="class" class="form-control">
                                @if($game->class == 0)
                                    <option value=0>网页免费版</option>
                                @elseif($game->class == 1)
                                    <option value=1>网页收费版</option>
                                @elseif($game->class == 2)
                                    <option value=2>安卓免费版</option>
                                @else
                                    <option value=3>安卓收费版</option>
                                @endif
                            </select>
                        </fieldset>
                    </label>
                </div>
                <div class="form-group">
                    <input type="hidden" id="id" value="{{$game->id}}">
                    <label class="col-lg-2 input-title-right"><i>*</i> 产品名称</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="name" name="name" placeholder="产品名称" value="{{$game->name}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i> 产品英文名称</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="en_name" name="name_en" placeholder="产品英文名称"
                               value="{{$game->name_en}}">
                    </label>
                </div>
                @if($game->class == 2 || $game->class == 3)
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>版本名</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="version" name="version"
                               placeholder="提供给外部显示的版本信息 如:10.01.12"
                               value="{{$game->version}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>版本号</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="version_no" name="version_no"
                               placeholder="用于平台检测APP版本 如：102" value="{{$game->version_no}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>APK大小</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="total_size" name="total_size"
                               placeholder="APK包的大小，以 KB 为单位" value="{{$game->total_size}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>包名</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="package_name" name="package_name"
                               placeholder="APK包的包名" value="{{$game->package_name}}">
                    </label>
                </div>
                @endif
                @if($game->class == 1 || $game->class == 3 )
                    <div class="form-group">
                        <label class="col-lg-2 input-title-right"><i>*</i> 产品公钥</label>
                        <div class="col-lg-8">
                            <input type="hidden" id="public_key" name="public_key" value="{{$ext->public_key}}">
                            <input type="file" id="key-file" style="width: 0;height: 0">
                            <button type="button" class="btn btn-primary btn-sm" onclick="$('#key-file').click()" >
                                点击 上传
                            </button>
                            <span class="msg">@if($ext->public_key) <label style="color: #2ab27b">已上传KEY</label> @endif</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 input-title-right"><i>*</i> 支付回掉地址</label>
                        <label class="col-lg-8">
                            <input type="text" class="form-control" id="notify_url" name="notify_url" placeholder="平台服务器回掉GAME SERVER URL   http://you host... "
                                   value="{{$ext->notify_url}}">
                        </label>
                    </div>
                @endif
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>ICON</label>
                    <div class="col-lg-8">
                        <div class="upload" id='case1'
                             action='{{url('/upload_img')}}'
                             del_url= '{{url('/delete_img')}}'
                             data-num=1
                             data-value="{{$game->icon}}"
                        ></div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>产品简介标题</label>
                    <label class="col-lg-8">
                        <input type="text" class="form-control" id="title" name="title" placeholder="概括性的对游戏进行说明"
                               value="{{$game->title}}">
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>产品简介内容</label>
                    <label class="col-lg-8">
                        <textarea class="form-control" id="synopsis" name="synopsis"
                                  placeholder="简单的、富有代表性的对游戏进行介绍">{{$game->synopsis}}</textarea>
                    </label>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 input-title-right"><i>*</i>游戏类型</label>
                    <label class="col-lg-8">
                        <select name="type" id="type"  class="form-control">
                                <option value=1 @if($game->class == 1)   selected="selected" @endif>角色扮演</option>
                                <option value=2 @if($game->class == 2)   selected="selected" @endif>桌面游戏</option>
                                <option value=3 @if($game->class == 3)   selected="selected" @endif>射击游戏</option>
                                <option value=4 @if($game->class == 4)   selected="selected" @endif>运动游戏</option>
                                <option value=5 @if($game->class == 5)   selected="selected" @endif>动作游戏</option>
                                <option value=6 @if($game->class == 6)   selected="selected" @endif>卡片游戏</option>
                                <option value=7 @if($game->class == 7)   selected="selected" @endif>冒险游戏</option>
                                <option value=8 @if($game->class == 8)   selected="selected" @endif>竞速游戏</option>
                                <option value=9 @if($game->class == 9)   selected="selected" @endif>益智游戏</option>
                                <option value=10 @if($game->class == 10) selected="selected" @endif>策略游戏</option>
                                <option value=11 @if($game->class == 11) selected="selected" @endif>音乐游戏</option>
                                <option value=12 @if($game->class == 12) selected="selected" @endif>网页游戏</option>
                        </select>
                    </label>
                </div>

            </div>

            <div class="form-group row-jf">
                <button type="submit" class="btn btn-success"> 提交</button>
                <button type="button" class="btn btn-danger" onclick="getData('{{url('/game_list')}}')"> 取消</button>
            </div>
        </form>
    </div>
</div>

<script>
    var classVal = {{$game->class}}
    $(function() {
        $("#case1").upload(
            function (_this, data) {

            }
        )
    })

    //密钥处理
    $('#key-file').change(function () {
        var files = this.files;
        var dom   = $(this);
        if (files.length) {
            var file = files[0];
            var reader = new FileReader();//new一个FileReader实例
            if (file.type === '') {//判断文件类型 当前无类型
                reader.onload = function() {
                    $('#public_key').val(reader.result)
                    dom.attr('disabled','disabled');
                    var html = '<span style="color: forestgreen;line-height: inherit;padding-left: 20px">上传成功</span>';
                    dom.parent().find('.msg').html(html);
                    return   false;
                };
                reader.readAsText(file);
            }
            var html = '<span style="color: red;line-height: inherit;padding-left: 20px">上传失败！请重试</span>';
            dom.parent().find('.msg').html(html);
        }
    })


    $(function () {

        var validation = {
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 8,
                        message: '名称长度必须在2到8位之间'
                    },
                }
            },
            name_en: {
                validators: {
                    notEmpty: {
                        message: '英文名称不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 20,
                        message: '英文名称长度必须在2到20位之间'
                    },
                }
            },
            title: {
                validators: {
                    notEmpty: {
                        message: '标题不能为空'
                    },
                    stringLength: {
                        min: 5,
                        max: 20,
                        message: '标题长度必须在5到20位之间'
                    },
                }
            },
            synopsis: {
                validators: {
                    notEmpty: {
                        message: '简介文本不能为空'
                    },
                    stringLength: {
                        min: 5,
                        max: 220,
                        message: '简介文本长度必须在5到220位之间'
                    },
                }
            },
            type:{
                validators: {
                    notEmpty: {
                        message: '游戏类型必须选择一项'
                    }
                }
            },
            upload:{
                validators: {
                    notEmpty: {
                        message: 'ICON不能为空'
                    }
                }
            },

        }

        if(classVal == 1 || classVal == 3) { //付费
            validation['public_key']= {
                validators: {
                    notEmpty: {
                        message: '请上传KEY'
                    }
                }
            };
            validation['notify_url']= {
                validators: {
                    notEmpty: {
                        message: '支付回掉地址不能为空'
                    }
                }
            };
        }
        if(classVal == 2 || classVal ==3){ // apk

            validation['total_size']= {
                validators: {
                    regexp: {//正则验证
                        regexp: /^[1-9]+[0-9]*$/,
                        message: '所输入的字符不符要求'
                    },
                    stringLength: {
                        max: 11,
                        message: '长度必须小于11位数字'
                    },
                }
            };
            validation['version']= {
                validators: {
                    notEmpty: {
                        message: '版本名不能为空'
                    }
                }
            };
            validation['version_no']= {
                validators: {
                    notEmpty: {
                        message: 'apk大小不能为空'
                    },
                    regexp: {//正则验证
                        regexp: /^[1-9]+[0-9]*$/,
                        message: '所输入的字符不符要求'
                    }
                }
            };
            validation['package_name']= {
                validators: {
                    notEmpty: {
                        message: '包名不能为空'
                    }
                }
            };
        }

        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: validation
        }).on("success.form.bv", function (e) {

            var icon = $('input[name="upload"]').val();
            if(!icon){
                layer.msg('请上传ICON',{icon: 5,time:1500});
                $('button[type=submit]').attr("disabled",false);
                return false;
            }

            let data = {
                id:$('#id').val(),
                name: $('#name').val(),
                name_en: $('#en_name').val(),
                version: $('#version').val(),
                version_no: $('#version_no').val(),
                title: $('#title').val(),
                synopsis: $('#synopsis').val(),
                total_size: $('#total_size').val(),
                class: $('#class').val(),
                icon:icon,
                type:$('#type').val(),
                package_name:$("#package_name").val(),
                public_key:$('#public_key').val(),
                notify_url:$('#notify_url').val(),
                _token: $('#_token').val()

            };

            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            $.ajax(
                {
                    method: 'post',
                    url: '{{URL::asset('/game_save')}}',
                    async: true,
                    dataType: 'text',
                    data: data,
                    success: function (data) {
                        layer.closeAll();
                        $('#page-content-wrapper').html(data)
                    },
                    error: function (err) {
                        console.log(err);
                        layer.closeAll();
                        layer.msg('错误!请重试!', {icon: 5, time: 1500})
                    }
                });

            return false;//阻止表单跳转

        });

    });

</script>
